//
//  YWDetail2TableViewController.m
//  百布业务
//
//  Created by 陈炳桦 on 15/11/4.
//  Copyright © 2015年 陈炳桦. All rights reserved.
//

#import "YWDetail2TableViewController.h"
#import <MJRefresh.h>
#import <MJExtension.h>
#import "YWHandleStatus.h"
#import "YWDetailHomeViewController.h"
#import "YWMyDemand.h"
#import "YWHandleStatusCell.h"

static NSString *ID = @"ID";

@interface YWDetail2TableViewController ()

// 模型数组
@property (nonatomic,strong) NSMutableArray *handleStatuses;

@end

@implementation YWDetail2TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化设置
    [self setUp];
    
    // 设置刷新
    [self setupRefresh];
}

- (void)setUp{
    
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YWHandleStatusCell class]) bundle:nil] forCellReuseIdentifier:ID];
    
    
    self.view.backgroundColor = BBRGBColor(234, 236, 238);
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    //去除下划线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
}

- (void)setupRefresh{
    // 下拉刷新
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewBuyerDemand)];
    [self.tableView.header beginRefreshing];
    
    
}

- (void)loadNewBuyerDemand{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    param[@"pageNum"] = @40;
    param[@"numPerPage"] = @0;
    
    YWMyDemand *mydemand = ((YWDetailHomeViewController *)self.parentViewController).mydemand;
    param[@"buyerDemandId"] = mydemand.ID;
    
    
    [YWNetwork handleHistorysWithParameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        self.handleStatuses = [YWHandleStatus objectArrayWithKeyValuesArray:responseObject[@"historys"]];
        
        [self.tableView reloadData];
        
        // 结束刷新
        [self.tableView.header endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 结束刷新
        [self.tableView.header endRefreshing];
    }];

}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.handleStatuses.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    YWHandleStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 取消选中状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.handleStatus = self.handleStatuses[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}


@end
